package com.imooc.user.controller;

import com.imooc.api.controller.user.AppUserMngControllerApi;
import com.imooc.api.controller.user.BaseControllerApi;
import com.imooc.enums.UserStatus;
import com.imooc.grance.result.GraceJsonResult;
import com.imooc.grance.result.ResponseStatusEnum;
import com.imooc.pojo.AppUser;
import com.imooc.user.service.AppUserMngService;
import com.imooc.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @auther Levi
 * @Date: 2021/03/21/21:23
 * @Description:
 */
@RestController
public class AppUserMngMngController extends BaseControllerApi implements AppUserMngControllerApi {
    @Autowired
    private AppUserMngService appUserMngService;
    @Override
    public GraceJsonResult queryAll(String nickname, Integer status,
                                    Date startDate, Date endDate,
                                    Integer page, Integer pageSize) {

//        System.out.println(startDate);
//        System.out.println(endDate);
        if (page == null) {
            page = COMMON_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }
        PagedGridResult pagedGridResult = appUserMngService.queryAllUserList(nickname,
                                    status,
                                    startDate,
                                    endDate,
                                    page,
                                    pageSize);
        return GraceJsonResult.ok(pagedGridResult);
    }

    @Override
    public GraceJsonResult userDetail(String userId) {
        AppUser userInfo = appUserMngService.getUserInfo(userId);
        return GraceJsonResult.ok(userInfo);
    }

    @Override
    public GraceJsonResult freezeUserOrNot(String userId, Integer doStatus) {
        //先判断前端传过来的用户的状态是否合法
        if (!UserStatus.isUserStatusValid(doStatus)) {
            return GraceJsonResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }
        appUserMngService.freezeUserOrNot(userId, doStatus);
        //用户正在登录状态，如果管理员更改了用户的状态，就需要清空用户的会话状态，用户就重新登录获取新的会话
        redis.del(REDIS_USER_INFO+":"+userId);
        return GraceJsonResult.ok();
    }

}
